﻿using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Markup;
using Microsoft.Win32;
using System.Net.Http;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace HearthHelper
{
	public class PushStone
	{
		//读取监控信息
		private static JObject ReadMonitorInfo(string path)
		{
			JObject postedJObject = new JObject();
			using (StreamReader file = File.OpenText(path))
			{
				
				using (JsonTextReader reader = new JsonTextReader(file))
				{
					//读取
					JObject jsonObject = (JObject)JToken.ReadFrom(reader);
					int Wins = (int)jsonObject["Wins"];
					int Losses = (int)jsonObject["Losses"];
					int Concedes = (int)jsonObject["Concedes"];
					int Level = (int)jsonObject["Level"];
					int Xp = (int)jsonObject["Xp"];
					int XpNeeded = (int)jsonObject["XpNeeded"];

					//组装
					int total = Wins + Losses;
					postedJObject.Add("战令等级", string.Format("{0}级", Level));
					postedJObject.Add("升级经验", string.Format("{0}/{1}", Xp, XpNeeded));
					postedJObject.Add("胜利场数", string.Format("{0}胜/{1}总", Wins, total));
					postedJObject.Add("对战胜率", total > 0 ? 
						string.Format("{0:0.00}%", 100.0f * Wins / total) :
						string.Format("0.00%"));

					file.Close();
				}
			}
			return postedJObject;
		}

		//组装成Json网络发送数据
		private static string StrToJsonSend(string token, string title, JObject content)
		{
			JObject postedJObject = new JObject();
			postedJObject.Add("token", token);
			postedJObject.Add("title", title);
			postedJObject.Add("content", content);
			postedJObject.Add("template", "json");
			return postedJObject.ToString();
		}

		//发送消息
		public enum MSG_TYPE{MSG_NORMAL, MSG_REBOOT, MSG_TEST, MSG_START, MSG_STOP, MSG_CHANGE};
		public static bool PostMessage(string token, string oldAccount,
			string account, int todayRebootCnt, int rebootMaxCnt,
			MSG_TYPE type, out string result)
		{
			string sendData="";

			//参数判断
			if (token.Length<=0)
			{
				result = "请先获取Token后重试";
				return false;
			}

			//组装消息
			if (type == MSG_TYPE.MSG_NORMAL)
			{
				JObject postedJObject = ReadMonitorInfo(
					"Settings/Default/Monitor.json");
				sendData = StrToJsonSend(token, "当前账号("+account+")运行正常", postedJObject);
			}
			else if (type == MSG_TYPE.MSG_REBOOT)
			{
				JObject postedJObject = new JObject();
				postedJObject.Add("当前状态", "异常重启");
				postedJObject.Add("重启次数", todayRebootCnt.ToString() + "/" + rebootMaxCnt.ToString());
				sendData = StrToJsonSend(token, "当前账号("+account+")运行异常", postedJObject);
			}
			else if (type == MSG_TYPE.MSG_TEST)
			{
				JObject postedJObject = new JObject();
				postedJObject.Add("测试", "这是一条测试推送");
				sendData = StrToJsonSend(token, "测试推送", postedJObject);
			}
			else if (type == MSG_TYPE.MSG_START)
			{
				JObject postedJObject = new JObject();
				postedJObject.Add("当前状态", "兄弟中控自动开始运行");
				postedJObject.Add("重启次数", todayRebootCnt.ToString() + "/" + rebootMaxCnt.ToString());
				sendData = StrToJsonSend(token, "兄弟中控已开始运行", postedJObject);
			}
			else if (type == MSG_TYPE.MSG_STOP)
			{
				JObject postedJObject = new JObject();
				postedJObject.Add("当前状态", "兄弟中控自动停止运行");
				postedJObject.Add("重启次数", todayRebootCnt.ToString() + "/" + rebootMaxCnt.ToString());
				sendData = StrToJsonSend(token, "兄弟中控已停止运行", postedJObject);
			}
			else if (type == MSG_TYPE.MSG_CHANGE)
			{
				JObject postedJObject = ReadMonitorInfo(
					"Settings/Default/Monitor.json");
				postedJObject.Add("之前账号", oldAccount);
				sendData = StrToJsonSend(token, "账号切换到(" + account + ")", postedJObject);
			}

			//初始化
			HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://pushplus.hxtrip.com/send");
			req.Method = "POST";
			req.ContentType = "application/json";
			byte[] data = Encoding.UTF8.GetBytes(sendData);
			req.ContentLength = data.Length;

			//发送请求
			using (Stream reqStream = req.GetRequestStream())
			{
				reqStream.Write(data, 0, data.Length);
				reqStream.Close();
			}
			HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
			Stream stream = resp.GetResponseStream();

			//获取响应内容
			using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
			{
				result = reader.ReadToEnd();
				if (result.Contains("200"))
				{
					result = "微信消息推送成功";
					return true;
				}
			}

			return false;
		}
	}
}